<template>
  <div class="report">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 主体 -->
    <el-card class="mt16">
      <!-- 图表的容器 -->
      <div class="myChart" ref="myChartDom"></div>
    </el-card>
  </div>
</template>

<script>
//引入echarts
import * as echarts from 'echarts'
import {getReportApi} from "../Api/index"
export default {
  name: 'report',
  data() {
    return {
      preChartData: {
        title: {
        //   text: '用户来源'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#E9EEF3'
            }
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            boundaryGap: false
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ]
      }
    }
  },
  mounted() {
    this.loadChart()
  },
  methods: {
    async loadChart() {
      //1.获取存放图表的容器
      const myChartDom = this.$refs.myChartDom
      //2.将chartDom初始化为echarts对象  -init
      const chart = echarts.init(myChartDom)

      //调用数据统计图表接口返回图表数据
      const res = await getReportApi()
      const mergeData = { ...this.preChartData, ...res.data }

      console.log('返回图表：', mergeData)

      //3.配置echarts图表对象
      const options = mergeData

      //4.渲染echarts图表  -  setOption
      chart.setOption(options)

      //5.图表随窗口尺寸改变而自适应
      window.addEventListener('resize', function () {
        chart.resize()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.myChart {
  width: 100%;
  height: 420px;
  border: 1px solid #ccc;
}
</style>
